const ImgDraw=(function(){
    /***
     * this.ctx是虚拟canvas上的画笔
     */
    var depl={};    //配置文件
    
    var depl_back={  //备份的配置文件    
        t:{//顶层配置
            pin:{
                color_t:"#b34141",//pin顶层焊盘颜色
                color_pt:"#83b31e",//pin顶层上钢网颜色
                color_st:"#83b31e",//pin顶层上阻焊颜色
                color_pb:"#83b31e",//pin顶层下钢网颜色
                color_sb:"#83b31e",//pin顶层下阻焊颜色
                color_hole:"#a5a0a0",//过孔颜色
                color_contour:"#99CCFF",   //pin描边颜色
                
            },
            text:{
                color_text:"black",//文字颜色
                color_border:"red",//文字范围
                typeface:"微软雅黑",//字体
                text_size:"10",//字号
            },
            path:{
                color_line:"#1a2273",//线颜色
                line_width:1,//线宽
            },
            shape:{
                color_fill:"green",//铜皮填充颜色
                color_out:"#83b31e",//铜皮镂空部分颜色 
                color_line:"while",//描边颜色
                line_width:1,//线宽
            },       
        },
        i:{//中层颜色配置
            pin:{
                color_t:"#b34141",//pin顶层焊盘颜色
                color_pt:"#83b31e",//pin顶层上钢网颜色
                color_st:"#83b31e",//pin顶层上阻焊颜色
                color_pb:"#83b31e",//pin顶层下钢网颜色
                color_sb:"#83b31e",//pin顶层下阻焊颜色
                color_hole:"#a5a0a0",//过孔颜色
                color_contour:"#99CCFF",   //pin描边颜色
                
            },
            text:{
                color_text:"black",//文字颜色
                color_border:"red",//文字范围
                typeface:"微软雅黑",//字体
                text_size:"10",//字号
            },
            path:{
                color_line:"#1a2273",//线颜色
                line_width:1,//线宽
            },
            shape:{
                color_fill:"green",//铜皮填充颜色
                color_out:"#83b31e",//铜皮镂空部分颜色
                color_line:"while",//描边颜色
                line_width:1,//线宽               
            },   
        },
        b:{//底层颜色配置
            pin:{
                color_t:"#b34141",//pin顶层焊盘颜色
                color_pt:"#83b31e",//pin顶层上钢网颜色
                color_st:"#83b31e",//pin顶层上阻焊颜色
                color_pb:"#83b31e",//pin顶层下钢网颜色
                color_sb:"#83b31e",//pin顶层下阻焊颜色
                color_hole:"#a5a0a0",//过孔颜色
                color_contour:"#99CCFF",   //pin描边颜色
                
            },
            text:{
                color_text:"black",//文字颜色
                color_border:"red",//文字范围
                typeface:"微软雅黑",//字体
                text_size:"10",//字号
            },
            path:{
                color_line:"#1a2273",//线颜色
                line_width:1,//线宽
            },
            shape:{
                color_fill:"green",//铜皮填充颜色
                color_out:"#83b31e",//铜皮镂空部分颜色 
                color_line:"while",//描边颜色
                line_width:1,//线宽
            },   
        },
        
        color_origin:"#a4bfa4",     //原点颜色
        typeface:"微软雅黑",            //全局字体 
        text_size:"20px",           //全局字号
        line_width:1,               //线宽
        line_color:"blue",
        txt_color:"black",

        highlight_color:"#ffbc00bd",  //强调的颜色
        highlight_scope:10,           //强调的范围
        grid_color:"#c0c0c0",       //格点颜色
    };
    var arr_dete={//数组映射
        pin:["fill","color","direction","color_line"],
        shape:["fill","color","color_line","stroke"],
        txt:["typeface","text_size","text_color"],
        path:["stroke"],
    };

    var tidy={//整理图形对象的配置属性
        pin:function(pin){//检测pin中的配置相关信息
            let arr=arr_dete.pin;
            arr.forEach(value=>{
                if(!pin[value]){
                    pin[value]=true;
                }
            });
            return pin;
        },
        txt:function(){

        },
        shape:function(){

        },
        path:function(){

        },
        isEmpty:function(obj){
            let lock=false;
            if(!obj){
                return true;
            }
            if(JSON.stringify(obj) === "{}"){
                lock=true;
            }
            if(Object.getOwnPropertyNames(obj).length==0){
                lock=true;
            }
            return lock;
        },
        //引脚上计算字体大小
        text_size:function(type,r,w,h){
            var size=depl.text_size;
            var area=0;
            switch(type){
                case 'C':case 'CIRCLE':
                case 'H_C':case 'CIRCLE_DRILL':
                    var area=3.14*r*r*1;
                    break;
                case 'S':case 'SQUARE':case "CS":
                case 'H_S':case 'SQUARE_DRILL':
                    var area=w*h*1;
                    break;
                case 'OC':
                case 'H_OC':
                    var area=3.14*r*r;
                    break;
                case 'OV':case 'OBLONG_Y':
                case 'H_OV':case 'OBLONG_X':
                    area=(w-h)*h;
                    break;
                case 'POLY':
                case 'H_POLY':
                    area=3.14*r*r;
                    break;
                case 'PATH':
                case 'H_PATH'://画笔，pin路径，坐标x，坐标y，旋转角度（pin整体+pin单层），偏移x，偏移y，颜色
                    if(w<h){
                        h=w; 
                    }else{
                        w=h;
                    }
                    area=w*h*1;
                    break;
                case 'nil':
                    break;
                /*default:
                    window.alert("pin图形绘制遗失"+h_form);
                    break;*/
                    
            }
            size = Math.sqrt(area) * 0.2;
            if(size <= 4){
                size = 0;
            }
            return size;
        },
    };
    //工具方法
    var tool={
        init:function(obj){//初始化
            if(!obj){
                return;
            }
            if(obj.deploy){//设置基本配置
                this.setDeploy(obj.deploy);
            }
            if(obj.origin){//设置原点
                this.setOrigin(obj.origin);
            }
            if(obj.prop){//设置格点
                this.setGrid(obj.prop);
            }
            
        },
        createCtx:function(){//创建画笔
            this.actual_canvas.width=this.actual_canvas.width;
            this.actual_ctx =this.actual_canvas.getContext("2d");
        },
        createVirCtx:function(){//创建虚拟画布画笔
            this.virtual_canvas=document.createElement('canvas'); // 创建一个新的canvas
            this.virtual_canvas.width= this.actual_canvas.width;
            this.virtual_canvas.height=this.actual_canvas.height;
            var ctx=this.virtual_canvas.getContext("2d");
            //Object.setPrototypeOf(ctx.prototype,tool);
            return ctx;
        },
        setOrigin:function(org){//设置原点
            if(!org){
                return;
            }
            this.origin=org;
            this.ctx.translate(this.origin.x,this.origin.y);
        },
        setDeploy:function(data){//设置配置
            if(!data){
                depl=depl_back;//备用配置
                return;
            }
            depl=data;
        },
        setGrid:function(grid){//设置格点
            if(!grid){
                return;
            }
            this.clear();
            var gridCtx=this.ctx;
           
            gridCtx.globalAlpha=0.5;
            gridCtx.save();
            //ctx.translate(this.data.canvas_wh[0]/2,this.data.canvas_wh[1]/2);
            gridCtx.strokeStyle = "black";
            gridCtx.lineWidth = 0.5;
            var x_max=gridCtx.canvas.width,
                y_max=gridCtx.canvas.height;
            let o_x=this.origin.x,
                o_y=this.origin.y;
            let gap= Math.sqrt(Math.pow((o_x-x_max/2),2)+Math.pow((o_y-y_max/2),2))
            var w=x_max/2,
                h=y_max/2,
                max = 1000;
            //gridCtx.translate(x_max/2,y_max/2);
            var max=1000;
            let size = max*grid/2;
            //以画布中心绘制格点
            for(let a=0,w=0;a<max/2;a+=1,w+=grid){
                gridCtx.beginPath();
                gridCtx.moveTo(w, -size);
                gridCtx.lineTo(w, size);
                gridCtx.stroke();
            }
            for(let a=0,w=0;a<max/2;a+=1,w-=grid){
                gridCtx.beginPath();
                gridCtx.moveTo(w, -size);
                gridCtx.lineTo(w, size);
                gridCtx.stroke();
            }
            for(let a=0,w=0;a<max/2;a+=1,w+=grid){
                gridCtx.beginPath();
                gridCtx.moveTo(-size, w);
                gridCtx.lineTo(size, w);
                gridCtx.stroke();
            }
            for(let a=0,w=0;a<max/2;a+=1,w-=grid){
                gridCtx.beginPath();
                gridCtx.moveTo(-size, w);
                gridCtx.lineTo(size, w);
                gridCtx.stroke();
            }
            gridCtx.restore(); 
            this.drawCanvas();
        },
        clear:function(){
            this.virtual_canvas.width=this.actual_canvas.width;
            this.actual_canvas.width= this.actual_canvas.width;
            this.setOrigin(this.origin);
            //this.drawCanvas();
        },
        redrawAll:function(obj){//绘制整体图形
            if(!obj){
                return;
            }
            if(tidy.isEmpty(depl)){
                depl=depl_back;
            }
            this.drawCanvas_ver(obj);
        },
        drawPins:function(pins){
            if(!pins||pins.length==0){
                return;
            }
            var txtlist=[];
            pins.forEach(pin=>{
                if(pin){
                    let obj={
                        form:pin.data.form,
                        fill:true,
                        stroke:false,
                        rotate:pin.data.rotate/1+pin.data.pRotate/1,
                        x:pin.data.x,
                        y:pin.data.y,

                        w:pin.data.w,
                        h:pin.data.h,
                        arc_r:pin.data.arc||true,
                        skewing_x:pin.data.skewing_x,
                        skewing_y:pin.data.skewing_y,
                        color:pin.fillStyle,
                        color_line:pin.strokeStyle,
                    };
                    if(obj.form=="C"){
                        obj.r=obj.w;
                    }
                    
                    if(!tidy.isEmpty(obj)&&obj.form){
                        switch(obj.form.toUpperCase()){
                            case "C":
                                this.circie(obj);
                                break;
                            case "S":
                                this.rectangle(obj);
                                break;
                            case "CS":
                                this.rectangle_c(obj);
                                break;
                            case "OV":
                                this.long_circle(obj);
                                break;
                            case "OC":
                                this.octagon(obj);
                                break;
                            case "POLY":
                                this.polygon(obj);
                                break;
                            case "PATH":
                                this.path_p(obj);
                                break;
                            default:
                                console.log("外径图形描述有异常:"+t_form);
                                break;
                        }
                         
                    }else{
                        console.log(pin);
                    }
                    var size=tidy.text_size(obj.form,obj.r,obj.w,obj.h);

                    var txt={
                        text_size:size,
                        color:"black",
                        content:pin.pin_num,
                        x:obj.x,
                        y:obj.y,
                        typeface:"微软雅黑",

                    }
                    
                    if(!size){
                        return;
                    }
                    txtlist[pin.pinid/1-1]=txt;
                }
                
            });
            //绘字
            if(txtlist.length>0){
                txtlist.forEach(txt=>{
                    this.text(txt);
                });
            }
        },
        drawPaths:function(paths){
            if(!paths||paths.length==0){
                return;
            }
            
            paths.forEach(path=>{
                if(!tidy.isEmpty(path)){//不是空对象
                    let line=path.data;
                    st_x=line.st_x/1,
                    st_y=line.st_y/1,
                    sp_x=line.sp_x/1,
                    sp_y=line.sp_y/1,
                    line_w=line.width/1,
                    r=line.r,
                    angle=line.arc_c,
                    line.color = path.strokeStyle;
                    w=line.w/1,
                    h=line.h/1,
                    x=line.o_x/1,
                    y=line.o_y/1;
                    
                    line.lineWidth=line_w;
                    let name=(path.data.name?path.data.name:"nil").toUpperCase();
                    if(name=="LINE"){
                        this.line(line);
                    }else if(name=="ARC"){
                        let begin=0,
                            end  =0,
                            mode=true;
                        if(st_x==sp_x&&st_y==sp_y){
                            begin=10;
                        }else{
                            mode=obj.arc_r;
                            begin=this.radian_verdict(st_x-x,st_y-y),
                            end  =this.radian_verdict(sp_x-x,sp_y-y);
                        }
                        line.x=x;
                        line.y=y;
                        line.r=r;
                        line.st=begin;
                        line.sp=end;
                        line.mode=line.arc_c=="t"?false:true;
                        this.arc_l(line);
                    }else if(name=="LS"){
                        line.r=r;
                        line.w=w;
                        line.h=h;
                        line.x=x;
                        line.y=y;
                        line.stroke=true;
                        this.rectangle_c(line);
                    }else if(name=="nil"){
                        console.error("路径有异常"+name);
                    }
                }
            });
        },
        drawShapes:function(shapes){
            if(!shapes||shapes.length==0){
                return;
            }
            var out_fun=()=>{
                //绘制out
                out.forEach(shape=>{
                    let obj={
                    };
                    if(shape.form=="S"){
                        obj=shape;
                    }else{
                        obj={
                            
                          color:shape.fillStyle,
                            color_line:shape.strokeStyle,
                            fill:true,
                        }
                    }
                    if(shape.form=="path"){
                        obj.path=shape.data;

                        this.path_l(obj);
                    }else if(shape.form=="S"){
                        this.rectangle(obj);
                    }else{

                    }
                });
            }
            var fill_fun=()=>{
                fill.forEach(shape=>{
                    let obj={
                    };
                    if(shape.form=="S"){
                        obj=shape;
                    }else{
                        obj={
                            
                          color:shape.fillStyle,
                            color_line:shape.strokeStyle,
                            fill:true,
                        }
                    }
                    if(shape.form=="path"){
                        obj.path=shape.data;
                        this.path_l(obj);
                    }else if(shape.form=="S"){
                        this.rectangle(obj);
                    }else{

                    }
                });
            }
            let out=[];
            let fill=[];
            shapes.forEach(shape=>{
                if(shape.make=="f"){
                    fill.push(shape);
                }else if(shape.make=="o"){
                    out.push(shape);
                }
            });
            this.ctx.save();
            
            fill_fun();
            this.ctx.globalAlpha=0.5;
            this.ctx.restore();
            this.ctx.save();
            this.ctx.globalAlpha=1;
            this.setCtxMode(6);
            out_fun();
            this.ctx.restore();
        },
         
        drawTexts:function(texts){
            if(!texts||texts.length==0){
                return;
            }
            texts.forEach(txt=>{
                if(txt.data&&!tidy.isEmpty(txt.data)){
                    let obj={
                        x:txt.data.x,
                        y:txt.data.y,
                        w:txt.data.textwidth ,
                        h:txt.data.textheight,
                        rot:txt.data.rotate||0,
                        skewing_x:txt.data.skewing_x,
                        skewing_y:txt.data.skewing_y,
                        text_size:txt.data.textblock||"5",
                        color:txt.fillStyle||"black",
                        typeface:txt.typeface||"微软雅黑",
                        content:txt.data.content,
                    };
                    var new_txt=this.text(obj);
                    txt.data.textwidth=new_txt.width;
                    txt.data.textheight=new_txt.height;
                }    
            });
        },
        setCtxMode:function(mode){
            var type="";
            switch(mode){
                case 1:
                    type="source-over";
                    break;
                case 2:
                    type="destination-in";
                    break;
                case 3:
                    type="source-in";
                    break;
                case 4:
                    type="source-out";
                    break;
                case 5:
                    type="lighter";
                    break;
                case 6:
                    type="xor";
                    break;
            }
            this.ctx.globalCompositeOperation=type;
            
        },
        drawCanvas_ver:function(obj){
            if(obj.pins&&obj.pins.length>0){
                this.ctx.save();
                this.drawPins(obj.pins);
                this.ctx.restore();
            }
            if(obj.paths&&obj.paths.length>0){
                this.ctx.save();
                this.drawPaths(obj.paths);
                this.ctx.restore();
            }
            if(obj.shapes&&obj.shapes.length>0){
                this.ctx.save();

                this.ctx.globalAlpha=0.5;
                
                this.drawShapes(obj.shapes);
                this.ctx.restore();
            }
            if(obj.texts&&obj.texts.length>0){
                this.ctx.save();
                this.drawTexts(obj.texts);
                this.ctx.restore();
            }
            
            this.drawCanvas();
        },

        drawCanvas:function(){//将虚拟canvas上的图绘到实际dom中
            this.actual_ctx.drawImage(this.virtual_canvas, 0,0,this.virtual_canvas.width,this.virtual_canvas.height);
        }
    };
    
    //标准图形方法
    var form_way={
        circie:function(obj){//圆形
            if(!obj){
                return;
            }
            let x=obj.x||0,
                r=obj.r||0;
            let y=obj.y||0;
            let skewing_x=obj.skewing_x||0,
                skewing_y=obj.skewing_y||0;
            let st=obj.st||0,
                sp=obj.sp||2;
            let dir=obj.arc_r||false,
                line_w=obj.line_w||1;
            let fill=obj.fill||true,
                stroke=obj.stroke||false;
            let color=obj.color,
                color_line=obj.color_line;
            
            x=x+skewing_x;
            y=y+skewing_y;
            
            this.ctx.save();
            this.ctx.lineWidth=line_w;
            this.ctx.beginPath();	
            this.ctx.arc(x,y,r/2,st,sp*Math.PI,dir);
            this.ctx.closePath();

            if(fill){
                this.ctx.fillStyle=color;
                this.ctx.fill();
            }
            if(stroke){
                this.ctx.strokeStyle=color_line;
                this.ctx.stroke();
            }
            this.ctx.restore();
        },
        rectangle:function(obj){//矩形
            if(!obj){
                return;
            }
            let x=obj.x||0,
                w=obj.w||0,
                h=obj.h||0;
            let y=obj.y||0,
                rot=obj.rotate||0;
            let skewing_x=obj.skewing_x||0,
                skewing_y=obj.skewing_y||0;
            let line_w=obj.line_w||1;
            let fill=obj.fill||true,
                stroke=obj.stroke||false;
            let color=obj.color,
                color_line=obj.color_line;
            
            x=x+skewing_x;
            y=y+skewing_y;
            rot=rot*Math.PI/180;
            this.ctx.save();
            this.ctx.beginPath();
            if(rot!=0) {
                this.ctx.translate(x,y);//修改原点到引脚中心
                this.ctx.rotate(rot);//旋转
                this.ctx.translate(-x,-y); // 平移
            }
            this.ctx.closePath();
            this.ctx.lineWidth = line_w||depl.line_width;
            if(fill){
               this.ctx.fillStyle=color||depl.layer_color;
               this.ctx.fillRect(x-w/2,y-h/2,w,h);
               this.ctx.fill();
            }
            if(stroke){
                this.ctx.strokeStyle=color_line||depl.p_line_color;
                this.ctx.strokeRect(x-w/2,y-h/2,w,h);
                this.ctx.stroke();
            }
            this.ctx.restore();    
                
        },
        rectangle_c:function(obj){//圆矩
            if(!obj){
                return;
            }
            let x=obj.x||0,
                w=obj.w||0,
                h=obj.h||0,
                r=obj.r||0,
                y=obj.y||0,
                rot=obj.rotate||0;
            if(r==0){
                this.rectangle(obj);
                return;
            }     
            let skewing_x=obj.skewing_x||0,
                skewing_y=obj.skewing_y||0;
            let line_w=obj.line_w||1;
            let fill=obj.fill||true,
                stroke=obj.stroke||false;
            let color=obj.color,
                color_line=obj.color_line;
            x=x+skewing_x;
            y=y+skewing_y;
            rot=rot*Math.PI/180;
            var x1=x -(w/2-r),
                y1=y-h/2,
                x2=x1+w-2*r,
                y2=y1,                              
                j1_x=x+w/2,   
                j1_y=y1,      
                                
                x3=x2+r,      
                y3=y2+r,      
                x4=x3,        
                y4=y3+h-2*r,  
                j2_x=x+w/2,   
                j2_y=y+h/2,   
                                
                x5=x2,        
                y5=y+h/2,     
                x6=x1,
                y6=y5 ,
                j3_x=x-w/2,
                j3_y=y+h/2,
                
                x7=x6-r,
                y7=y6-r,
                x8=x7,
                y8=y3,
                j4_x=x-w/2,
                j4_y=y-h/2;
            if(r<0){
                r=-r;  
                x1=x -(w/2-r);
                y1=y-h/2;
                x2=x -(w/2-r)+w-2*r;
                y2=y1;
                x3=x2+r;
                y3=y2+r;
                j1_x=x2;   
                j1_y=y1+r;
                
                x4=x3;
                y4=y3+h-2*r;
                x5=x2;
                y5=y+h/2;
                j2_x=x5;  
                j2_y=y4;
                
                x6=x1;
                y6=y5;
                x7=x6-r;
                y7=y6-r;
                j3_x=x6;   
                j3_y=y6-r;
                
                x8=x7;
                y8=y3;
                j4_x=x1;   
                j4_y=y8;
            }
            this.ctx.save();   
            this.ctx.beginPath();
            this.ctx.lineCap="round";
            this.ctx.lineJoin="round";
            this.ctx.lineWidth = line_w||depl.line_width;
            this.ctx.translate(x,y);//修改原点到引脚中心
            this.ctx.rotate(rot);//旋转
            this.ctx.translate(-x,-y); // 平移
            this.ctx.moveTo(x1,y1);
            this.ctx.lineTo(x2,y2);
            this.ctx.arcTo(j1_x, j1_y,x3, y3, r);
            this.ctx.lineTo(x4,y4);
            this.ctx.arcTo(j2_x, j2_y,x5,y5, r);
            this.ctx.lineTo(x6,y6);
            this.ctx.arcTo(j3_x, j3_y,x7, y7, r);
            this.ctx.lineTo(x8,y8);
            this.ctx.arcTo(j4_x, j4_y,x1,y1, r);
            if (fill) {
                this.ctx.fillStyle=color||_this.data.img_deploy.layer_color;
                this.ctx.fill();
            }
            
            if (stroke) {
                this.ctx.strokeStyle=color_line||_this.data.img_deploy.p_line_color;
                this.ctx.stroke();
            }
            this.ctx.restore();   
                
        },
        octagon:function(obj){//八角
            if(!obj){
                return;
            }
            obj.num=8;
            polygon(obj);
        },
        long_circle:function(obj){//长圆
            if(!obj){
                return;
            }
            let x=obj.x||0,
                w=obj.w||0,
                h=obj.ht||0,
                r_x=obj.w||0,
                r_y=obj.h||0,
                y=obj.y||0,
                rot=obj.rotate||0;
                
            let skewing_x=obj.skewing_x||0,
                skewing_y=obj.skewing_y||0;
            let st=obj.st||0,
                sp=obj.sp||2;
            let dir=obj.direction||false,
                line_w=obj.line_w||1;
            let fill=obj.fill||true,
                stroke=obj.stroke||false;
            let color=obj.color,
                color_line=obj.color_line;
            x=x+skewing_x;
            y=y+skewing_y;
            var width=r_x-r_y,
                height=r_y,
                arc_r=height/2,
                start=x-width/2,
                stop=y-height/2;
            if(r_x<r_y){
                width=r_y-r_x,
                height=r_x,
                arc_r=height/2,
                start=x-width/2,
                stop=y-height/2;
                rot=rot+90;
            }
            rot=rot*Math.PI/180;
            this.ctx.save();
            this.ctx.beginPath();
            this.ctx.translate(x,y);//修改原点到引脚中心
            this.ctx.rotate(rot);//旋转
            this.ctx.translate(-x,-y); // 平移s
            this.ctx.moveTo(start,stop);
            this.ctx.lineTo(start+width,stop); 
            this.ctx.arcTo(start+width+arc_r,stop,start+width+arc_r,stop+height,arc_r);   // 创建一个弧
            this.ctx.arcTo(start+width+arc_r,stop+height,start,stop+height,arc_r);   // 创建一个弧
            this.ctx.lineTo(start,stop+height);
            this.ctx.arcTo(start-arc_r,stop+height,start-arc_r,stop,arc_r);
            this.ctx.arcTo(start-arc_r,stop,start,stop,arc_r);
            this.ctx.closePath();
            this.ctx.lineWidth = line_w||1;
            if (fill) {
                this.ctx.fillStyle=color||_this.data.img_deploy.layer_color;
                this.ctx.fill();
            }
            if (stroke) {
                this.ctx.strokeStyle=color_line||_this.data.img_deploy.p_line_color;
                this.ctx.stroke();
            }
            this.ctx.restore();
        },
        polygon:function(obj){//正多边
            if(!obj){
                return;
            }
            let num=obj.num,
                x=obj.x||0,
                r=obj.r||0,
                y=obj.y||0,
                rot=obj.rotate||0;
            let skewing_x=obj.skewing_x||0,
                skewing_y=obj.skewing_y||0;
            let line_w=obj.line_w||1;
            let fill=obj.fill||true,
                stroke=obj.stroke||false;
            let color=obj.color,
                color_line=obj.color_line;
            x=x+skewing_x;
            y=y+skewing_y;
            rot=rot*Math.PI/180;
            var startX = x + r * Math.cos(2*Math.PI*0/num);
            var startY = y + r * Math.sin(2*Math.PI*0/num);
            this.ctx.save();
            this.ctx.lineWidth=line_w;
            this.ctx.translate(x,y);//修改原点到引脚中心
            this.ctx.rotate(rot);//旋转
            this.ctx.translate(-x,-y); // 平移
            //开始路径
            this.ctx.beginPath();
            this.ctx.moveTo(startX, startY);
            for(var i = 1; i <= num; i++) {
                var newX = x + r * Math.cos(2*Math.PI*i/num);
                var newY = y + r * Math.sin(2*Math.PI*i/num);
                this.ctx.lineTo(newX, newY);
            }
            this.ctx.closePath();
            this.ctx.lineWidth = 0;
            this.ctx.lineJoin = 'round';
            if (fill) {
                this.ctx.fillStyle=color||_this.data.img_deploy.layer_color;
                this.ctx.fill();
            }
            
            if (stroke) {
                this.ctx.strokeStyle=color_line||_this.data.img_deploy.p_line_color;
                this.ctx.stroke();
            }
            this.ctx.restore();
        },
        path_p:function(obj){//路径有中心点
            if (!obj||!obj.path||obj.path.length==0) {
                return;
            }
            let color = obj.color || depl.layer_color,
                color_line = obj.color.color_line || depl.line_color,
                stroke = obj.stroke || true,
                fill = obj.fill || false;
            let rot = obj.rot || 0,
                x = obj.x,
                y = obj.y,
                r=0,
                skewing_x = obj.skewing_x,
                skewing_y = obj.skewing_y;
            let o_x = 0,
                o_y = 0,
                name = '',
                st_x = '',
                st_y = '',
                sp_x = '',
                sp_y = '',
                head_x = [],
                head_y = [],
                img_num = 0,
                mode = '',
                lock = "",
                begin = 0,
                end = 0;
            let buff_x = 0,
                buff_y = 0,
                buff_lock = "";
            if (rot == 90 || rot == 270) {
                rot = -rot * Math.PI / 180;
            } else {
                rot = rot * Math.PI / 180;
            }
            x = x + skewing_x;
            y = y + skewing_y;
            this.ctx.save();
            this.ctx.translate(x,y);
            this.ctx.lineCap = "round";
            this.ctx.lineJoin = "round";
            this.ctx.rotate(rot);//旋转
            this.ctx.beginPath();
            
            obj.path.forEach(path => {
                if (path) {
                    name = a_d.name;
                    name = name.toUpperCase();
                    o_x = path.o_x;
                    o_y = path.o_y;
                    st_x = path.st_x;
                    st_y = path.st_y;
                    sp_x = path.sp_x;
                    sp_y = path.sp_y;
                    mode = path.arc_r;
                    line_w = path.width || depl.line_width;
                    if (a == 0) {
                        buff_x = sp_x;
                        buff_y = sp_y;
                    }
                    if (line_w < 1) {
                        line_w = 1;
                    }
                    this.ctx.lineWidth = line_w;
                    if (name == "ARC") {//控制弧线方向
                        //console.log(st_x,st_y,sp_x,sp_y,o_x,o_y,r);
                        if (st_x == sp_x && st_y == sp_y) {
                            lock = "c";
                            begin = 10;
                            mode = true;
                        } else {
                            begin = this.radian_verdict(st_x - o_x, st_y - o_y);
                            end = this.radian_verdict(sp_x - o_x, sp_y - o_y);
                        }
                    }
                    //img_num新图形标志
                    if (img_num == 0) {
                        img_num += 1;
                        if (name == "LINE") {
                            this.ctx.moveTo(st_x, st_y);
                            this.ctx.lineTo(sp_x, sp_y);
                        } else if (name == "ARC") {
                            this.ctx.moveTo(st_x, st_y);
                            //ctx.arc(o_x,o_y,r,begin/3.14*Math.PI,end/3.14*Math.PI,mode);
                            this.ctx.arc(o_x, o_y, r, begin, end, mode);
                            if (lock == "c") {
                                img_num = 0;
                                this.ctx.closePath();
                            }
                        }
                    } else {
                        //该判断现在的结束点是否在已知线段上
                        let lock_q = 0,
                            j = 1;
                        for (var i = 0; i <= head_x.length; i++) {//判断一个图形是不是结束了
                            if (sp_x == head_x[i] && sp_y == head_y[i]) {
                                lock_q += 1;
                                break;
                            }
                        }
                        if (lock_q > 0) {//尾坐标等于首坐标时判定图形闭合
                            if (name == "LINE") {
                                this.ctx.lineTo(sp_x, sp_y);
                            } else if (name == "ARC") {
                                this.ctx.arc(o_x, o_y, r, begin, end, mode);
                            }
                            img_num = 0;
                            this.ctx.closePath();
                        } else {//绘制过程中
                            if (name == "LINE") {
                                this.ctx.lineTo(sp_x, sp_y);
                            } else if (name == "ARC") {
                                this.ctx.arc(o_x, o_y, r, begin, end, mode);
                                if (lock == "c") {
                                    img_num = 0;
                                    this.ctx.closePath();
                                }
                            }
                        }

                    }
                    head_x[head_x.length] = st_x;
                    head_y[head_y.length] = st_y;
                    head_x[head_x.length] = sp_x;
                    head_y[head_y.length] = sp_y;
                }
            });
            this.ctx.closePath();
            if (stroke) {
                this.ctx.strokeStyle = color_line ;
                this.ctx.stroke();
            }
            if (fill) {
                this.ctx.fillStyle = color;
                this.ctx.fill();
            }
            
            this.ctx.translate(-x, -y); // 平移
            this.ctx.restore();
        },
        path_l: function (obj) {//路径无中心点
            if (!obj || !obj.path || obj.path.length == 0) {
                return;
            }
            let color = obj.color || depl.layer_color,
                color_line = obj.color.color_line || depl.line_color,
                stroke = obj.stroke || true,
                fill = obj.fill || false;
            let r = 0;
            let o_x = 0,
                o_y = 0,
                name = '',
                st_x = '',
                st_y = '',
                sp_x = '',
                sp_y = '',
                head_x = [],
                head_y = [],
                img_num = 0,
                mode = '',
                lock = "",
                begin = 0,
                end = 0;
            let buff_x = 0,
                buff_y = 0,
                buff_lock = "";
            this.ctx.save();
            this.ctx.lineCap = "round";
            this.ctx.lineJoin = "round";
            this.ctx.beginPath();

            obj.path.forEach(path => {
                if (path) {
                    name = path.name;
                    name = name.toUpperCase();
                    o_x = path.o_x;
                    o_y = path.o_y;
                    st_x = path.st_x;
                    st_y = path.st_y;
                    sp_x = path.sp_x;
                    sp_y = path.sp_y;
                    mode = path.arc_r;
                    line_w = path.width || depl.line_width;
                    if (a == 0) {
                        buff_x = sp_x;
                        buff_y = sp_y;
                    }
                    if (line_w < 1) {
                        line_w = 1;
                    }
                    this.ctx.lineWidth = line_w;
                    if (name == "ARC") {//控制弧线方向
                        //console.log(st_x,st_y,sp_x,sp_y,o_x,o_y,r);
                        if (st_x == sp_x && st_y == sp_y) {
                            lock = "c";
                            begin = 10;
                            mode = true;
                        } else {
                            begin = this.radian_verdict(st_x - o_x, st_y - o_y);
                            end = this.radian_verdict(sp_x - o_x, sp_y - o_y);
                        }
                    }
                    //img_num新图形标志
                    if (img_num == 0) {
                        img_num += 1;
                        if (name == "LINE") {
                            this.ctx.moveTo(st_x, st_y);
                            this.ctx.lineTo(sp_x, sp_y);
                        } else if (name == "ARC") {
                            this.ctx.moveTo(st_x, st_y);
                            //ctx.arc(o_x,o_y,r,begin/3.14*Math.PI,end/3.14*Math.PI,mode);
                            this.ctx.arc(o_x, o_y, r, begin, end, mode);
                            if (lock == "c") {
                                img_num = 0;
                                this.ctx.closePath();
                            }
                        }
                    } else {
                        //该判断现在的结束点是否在已知线段上
                        let lock_q = 0,
                            j = 1;
                        for (var i = 0; i <= head_x.length; i++) {//判断一个图形是不是结束了
                            //console.log((sp_y-head_y[i])/(head_y[j]-head_y[i]));
                            //console.log((sp_x-head_x[i])/(head_x[j]-head_x[i]));
                            if (sp_x == head_x[i] && sp_y == head_y[i]) {
                                lock_q += 1;
                                break;
                            }
                        }
                        if (lock_q > 0) {//尾坐标等于首坐标时判定图形闭合
                            if (name == "LINE") {
                                this.ctx.lineTo(sp_x, sp_y);
                            } else if (name == "ARC") {
                                this.ctx.arc(o_x, o_y, r, begin, end, mode);
                            }
                            img_num = 0;
                            this.ctx.closePath();
                        } else {//绘制过程中
                            if (name == "LINE") {
                                this.ctx.lineTo(sp_x, sp_y);
                            } else if (name == "ARC") {
                                this.ctx.arc(o_x, o_y, r, begin, end, mode);
                                if (lock == "c") {
                                    img_num = 0;
                                    this.ctx.closePath();
                                }
                            }
                        }

                    }
                    head_x[head_x.length] = st_x;
                    head_y[head_y.length] = st_y;
                    head_x[head_x.length] = sp_x;
                    head_y[head_y.length] = sp_y;
                }
            });
            this.ctx.closePath();
            if (stroke) {
                this.ctx.strokeStyle = color_line;
                this.ctx.stroke();
            }
            if (fill) {
                this.ctx.fillStyle = color;
                this.ctx.fill();
            }

            this.ctx.restore();
        },
        line: function (obj) {
            if(!obj){
                return;
            }
            let x_st=obj.st_x,
                y_st=obj.st_y,
                x_sp=obj.sp_x,
                y_sp=obj.sp_y;
            let line_w=obj.lineWidth||depl.line_width,
                color=obj.color||depl.line_color;
            this.ctx.save();
            this.ctx.beginPath();
            this.ctx.lineWidth=line_w||3;
            this.ctx.lineCap="round";
            this.ctx.lineJoin="round";
            this.ctx.strokeStyle = color;
            this.ctx.moveTo(x_st,y_st);
            this.ctx.lineTo(x_sp,y_sp);
            this.ctx.stroke();
            this.ctx.closePath();
            this.ctx.restore();
        },
        arc:function(obj){
            if(!obj){
                return;
            }
            let rot=obj.rotate,
                x=obj.x,
                y=obj.y,
                angle=obj.angle;
            let color =obj.color,
                dir=obj.direction||false,
                line_w=obj.line_w||depl.lineWidth;
                
            if(Math.abs(angle)>360){//如果输入弧度值大于圆则取余
                angle=angle%360;
            }
            var angle_st=0,
                angle_sp=0;    
            
            
            //console.log(r,angle);
            if(angle>0&&r>0){//第四象限
                angle_sp=angle;
            }else if(angle<0&&r>0){//第三象限
                angle_st=90;
                angle_sp=-angle+angle_st;
            }else if(angle>0&&r<0){//第一象限
                angle_sp=angle;
            }else if(angle<0&&r<0){//第二象限
                angle_st=90;
                angle_sp=-angle+angle_st;
            }
            if(r<0){
                r=-r;
                dir=true;//逆时针
                angle_st=-angle_st;
                angle_sp=-angle_sp;
            }
            rot=rot*Math.PI/180;
            this.ctx.save();
            this.ctx.translate(x,y);//修改原点到引脚中心
            this.ctx.rotate(rot);//旋转
            this.ctx.translate(-x,-y); // 平移
            this.ctx.lineWidth=line_w||_this.data.img_deploy.line_width;
            this.ctx.strokeStyle =color||_this.data.img_deploy.line_color;
            this.ctx.beginPath();	
            this.ctx.arc(x,y,r,angle_st*Math.PI/180,angle_sp*Math.PI/180,dir);
            this.ctx.stroke();
            this.ctx.restore();
            
        },
        arc_l:function(obj){
            let x=obj.x,
                y=obj.y,
                st=obj.st,
                sp=obj.sp;
            let dir=obj.mode,
                line_w=obj.lineWidth,
                color=obj.color;

            this.ctx.save();
            this.ctx.lineWidth=line_w||depl.line_width;
            this.ctx.strokeStyle =color||depl.line_color;
            this.ctx.beginPath();	
            this.ctx.arc(x,y,r,st/3.14*Math.PI,sp/3.14*Math.PI,dir);
            this.ctx.stroke();
            this.ctx.restore();
        },
        text:function(obj){
            if(!obj){
                return;
            }
            let x=obj.x,
                y=obj.y,
                rot=obj.rot,
                skewing_x=obj.skewing_x/1||0,
                skewing_y=obj.skewing_y/1||0,
                text_size=obj.text_size||depl.text_size,
                color=obj.color||depl.text_color,
                typeface=obj.typeface||depl.typeface;
            let txt=obj.content;
            x=x/1+skewing_x/1;
            y=y/1+skewing_y/1;
            rot=rot*Math.PI/180;

           
            this.ctx.save();

            if(rot!=0){
                this.ctx.translate(x,y);//修改原点到引脚中心
                this.ctx.rotate(rot);//旋转
                this.ctx.translate(-x,-y); // 平移
            }

            this.ctx.fillStyle=color;//字体颜色
            this.ctx.textBaseline="middle";
            this.ctx.strokeStyle =color;//设置边框颜色
            //" style variant weight 12px arial"
            this.ctx.font = "normal normal bold "+text_size+"px "+typeface; 
            this.ctx.textAlign = "center"; 
            this.ctx.fillText(txt, x, y);
            var measure = this.ctx.measureText(txt,x,y);//文本对象
            this.ctx.restore();

            return measure;
        },
        radian_verdict:function(x,y){//获取弧度
            var angle=0;
            if(x>0&&y>=0){
                angle=Math.atan(y/x);
            }else if(x<=0&&y>0){
                x=Math.abs(x);
                angle=Math.atan(x/y)+0.5*Math.PI;
            }else if(x<0&&y<=0){
                x=Math.abs(x);
                y=Math.abs(y);
                angle=Math.atan(y/x)+Math.PI;
            }else if(x>=0&&y<0){
                y=Math.abs(y);
                angle=Math.atan(x/y)+1.5*Math.PI;
            }
            return angle;
        },
        
    };
    function draw(canvas){
        this.actual_canvas=canvas; //实际画布
        //draw.prototype=img_create;
        Object.setPrototypeOf(tool,form_way);
        Object.setPrototypeOf(draw.prototype,tool);
        this.createCtx();//最终图形画笔
        ctx=this.createVirCtx();//虚拟画笔
        this.ctx=ctx;
    };
    return draw;
})();



    
    